from google.appengine.ext import db, webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import simplejson as json
#import base64
class GetAll(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        types = db.GqlQuery("SELECT * from DishType")
        result = [{'key': str(dish_type.key()), 'name': str(dish_type.name)} for dish_type in types]
        self.response.out.write(json.dumps(result))

class GetKeyByName(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write("in GET");
            
    def post(self):
        self.response.headers['Content-Type'] = 'text/plain'
        dishTypeName = self.request.get("dishTypeName");
        query = db.GqlQuery("SELECT __key__ FROM DishType WHERE name = :1 LIMIT 1", dishTypeName);
        if query.count(1) != 0:
            self.response.out.write(query[0]);
        else:
            self.response.out.write("Not found");
        
application = webapp.WSGIApplication([('/dish_types/all', GetAll), ('/dish_types/get_key', GetKeyByName)], debug=True)
  
def main():
    run_wsgi_app(application)
    
if __name__ == "__main__":
    main()
